<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use Auth;
class SessionsController extends Controller
{

     public function __construct()
     {
        $this->middleware('guest',[
          'only'=>['create']
        ]);

     }



    public function create()
    {

      return view('sessions.create');
    }

    public function store(Request $request)
    {
        $credentials=$this->validate($request,[
           'email'=>"required|email|max:225",
           'password'=>'required',
        ]);
        //login Todo
       if(Auth::attempt($credentials,$request->has('remember'))){
          if(Auth::user()->activated){
            session()->flash('success', '欢迎回来！');
            return redirect()->intended(route('users.show', [Auth::user()]));
          }else{
            Auth::logout();
            session()->flash('waring',"你的账号未激活，请检查邮箱中的注册邮件进行激活。");
            return redirect('/');
         }
       }else{
          session()->flash('danger','您输入邮箱或密码有错误');
          return redirect()->back();
       }

    }

     public function destroy()
     {
         Auth::logout();
         session()->flash('success',"退出登陆");
         return redirect('/');

     }




}
